package com.atguigu.spzx.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.common.constant.OrderStatusEnum;
import com.atguigu.spzx.common.constant.PaymentStatusEnum;
import com.atguigu.spzx.order.dto.OrderInfoDetailRespDto;
import com.atguigu.spzx.order.entity.OrderInfo;
import com.atguigu.spzx.order.feign.OrderFeignClinet;
import com.atguigu.spzx.pay.entity.PaymentInfo;
import com.atguigu.spzx.pay.mapper.PaymentInfoMapper;
import com.atguigu.spzx.pay.service.PaymentInfoService;
import com.atguigu.spzx.product.dto.SkuSaleNumUpdateReqDto;
import com.atguigu.spzx.product.feign.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * PaymentInfoServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2023/11/6
 */
@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private OrderFeignClinet orderFeignClinet;
    @Autowired
    private ProductFeignClient productFeignClient;

    /**
     * 保存支付记录
     * @param orderNo
     * @return
     */
    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        PaymentInfo paymentInfo = paymentInfoMapper.getPaymentInfoByOrderNo(orderNo);

        //支付记录不存在
        if (paymentInfo == null) {
            //远程调用获取订单信息
            OrderInfoDetailRespDto orderInfo = orderFeignClinet.getOrderInfoByOrderNo(orderNo);
            //封装支付记录
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setPayType(orderInfo.getPayType());
            String content = "";
            for(OrderInfoDetailRespDto.OrderInfoDetailInnerOrderItem item : orderInfo.getOrderItemList()) {
                content += item.getSkuName() + " ";
            }
            paymentInfo.setContent(content);
            paymentInfo.setAmount(orderInfo.getTotalAmount());
            paymentInfo.setOrderNo(orderNo);
            paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getCode());
            //保存支付记录
            paymentInfoMapper.insert(paymentInfo);
        }
        return paymentInfo;
    }

    /**
     * 支付完成，更新状态
     * @param map
     */
    @Override
    public void updatePaymentStatus(Map<String, String> map) {
        //根据订单号查询支付记录信息
        PaymentInfo paymentInfo = paymentInfoMapper.getPaymentInfoByOrderNo(map.get("out_trade_no"));

        //如果支付记录已经完成，不需要更新
        if (PaymentStatusEnum.PAID.getCode().equals(paymentInfo.getPaymentStatus())) {
            return;
        }

        //没有完成才进行更新
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getCode());
        paymentInfo.setOutTradeNo(map.get("trade_no"));
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(map));
        paymentInfoMapper.updateById(paymentInfo);


        //更新订单状态
        orderFeignClinet.updateOrderStatus(paymentInfo.getOrderNo(), OrderStatusEnum.PENDING_SHIPMENT.getCode());

        //更新销量
        OrderInfoDetailRespDto orderInfoByOrderNo = orderFeignClinet.getOrderInfoByOrderNo(paymentInfo.getOrderNo());
        List<SkuSaleNumUpdateReqDto> skuSaleDtoList = orderInfoByOrderNo.getOrderItemList().stream().map(item -> {
            SkuSaleNumUpdateReqDto skuSaleDto = new SkuSaleNumUpdateReqDto();
            skuSaleDto.setSkuId(item.getSkuId());
            skuSaleDto.setSkuNum(item.getSkuNum());
            return skuSaleDto;
        }).collect(Collectors.toList());
        productFeignClient.updateSkuSaleNum(skuSaleDtoList) ;
    }
}
